<?php

/**
 * This is the model class for table "trade_product".
 *
 * The followings are the available columns in table 'trade_product':
 * @property string $productId
 * @property string $name
 * @property string $description
 * @property string $smallImgUrl
 * @property string $bigImgUrl
 * @property string $price
 * @property string $cost
 * @property string $createdUserId
 * @property string $createdDate
 * @property string $updatedUserId
 * @property string $updatedDate
 * @property string $viewCount
 * @property integer $status
 * @property string $productCategoryId
 * @property integer $isDeleted
 *
 * The followings are the available model relations:
 * @property Productcategory $productCategory
 * @property HrmUser $createdUser
 * @property HrmUser $updatedUser
 * @property Productcomment[] $productcomments
 * @property Producttag[] $tradeProducttags
 */
class Product extends BaseDateUserModel
{
        public $file_upload_small;
        public $file_upload_big;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'pjm_book';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, description, price, cost, quantity, hardCover, publisher, language, author, publicationDate, dimensions', 'required'),
                        array('file_upload_small, file_upload_big', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true),
			array('status, isDeleted', 'numerical', 'integerOnly'=>true),
			array('name, smallImgUrl, bigImgUrl, price, cost', 'length', 'max'=>255),
			array('createdUserId, updatedUserId, viewCount, productCategoryId', 'length', 'max'=>20),
			array('createdDate, updatedDate', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('productId, name, description, smallImgUrl, bigImgUrl, price, cost, createdUserId, createdDate, updatedUserId, updatedDate, viewCount, status, productCategoryId, isDeleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'productCategory' => array(self::BELONGS_TO, 'Productcategory', 'productCategoryId'),
			'createdUser' => array(self::BELONGS_TO, 'HrmUser', 'createdUserId'),
			'updatedUser' => array(self::BELONGS_TO, 'HrmUser', 'updatedUserId'),
			'productcomments' => array(self::HAS_MANY, 'Productcomment', 'productId'),
			'tradeProducttags' => array(self::MANY_MANY, 'Producttag', 'trade_producttagmapping(productId, tagId)'),
                        'commentCount' => array(self::STAT, 'BookComment', 'commentId'),
                        'comments' => array(self::HAS_MANY, 'BookComment', 'productId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'productId' => 'ID',
			'name' => 'Name',
			'description' => 'Description',
			'smallImgUrl' => 'Small Img Url',
			'bigImgUrl' => 'Big Img Url',
			'price' => 'Price',
			'cost' => 'Cost',
                        'quantity' => 'Quantity',
                        'hardCover' => 'Hard Cover',
                        'publisher' => 'Publisher',
                        'language' => 'Language',
                        'author' => 'Author',
                        'publicationDate' => 'Publication Date',
                        'dimensions' => 'Dimensions',
			'createdUserId' => 'Created User',
			'createdDate' => 'Created Date',
			'updatedUserId' => 'Updated User',
			'updatedDate' => 'Updated Date',
			'viewCount' => 'View Count',
			'status' => 'Status',
			'productCategoryId' => 'Product Category',
			'isDeleted' => 'Is Deleted',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('productId',$this->productId,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('smallImgUrl',$this->smallImgUrl,true);
		$criteria->compare('bigImgUrl',$this->bigImgUrl,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('cost',$this->cost,true);
		$criteria->compare('createdUserId',$this->createdUserId,true);
		$criteria->compare('createdDate',$this->createdDate,true);
		$criteria->compare('updatedUserId',$this->updatedUserId,true);
		$criteria->compare('updatedDate',$this->updatedDate,true);
		$criteria->compare('viewCount',$this->viewCount,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('productCategoryId',$this->productCategoryId,true);
		$criteria->compare('isDeleted',$this->isDeleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
	public function searchIsTrashed($isTrashed)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('productId',$this->productId,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('smallImgUrl',$this->smallImgUrl,true);
		$criteria->compare('bigImgUrl',$this->bigImgUrl,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('cost',$this->cost,true);
		$criteria->compare('createdUserId',$this->createdUserId,true);
		$criteria->compare('createdDate',$this->createdDate,true);
		$criteria->compare('updatedUserId',$this->updatedUserId,true);
		$criteria->compare('updatedDate',$this->updatedDate,true);
		$criteria->compare('viewCount',$this->viewCount,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('productCategoryId',$this->productCategoryId,true);
		$criteria->compare('isDeleted',$isTrashed);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getAllProducts(){
            $allBooks = CHtml::listData(Product::model()->findAll(), 'productId', 'name');
            return $allBooks;
        }
}